/**
 * 
 */
package com.hj.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import com.hj.dao.AccountManager;
import com.hj.entity.AcctUser;
import com.hj.service.CommonService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author huangjian
 */
public class UserAction extends ActionSupport {
	private static final long serialVersionUID = -6851143392294872661L;
	private Logger log = LogManager.getLogger(getClass());
	@Autowired
	private AccountManager accountManager;

	@Autowired
	private CommonService commonService;

	@PersistenceContext
	private EntityManager em;

	private List<AcctUser> users = new ArrayList<AcctUser>();
	private Page<AcctUser> page;

	public Page<AcctUser> getPage() {
		return page;
	}

	public List<AcctUser> getUsers() {
		return users;
	}

	@Override
	public String execute() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("loginName", "huangjian");
		List<AcctUser> li = commonService.findBy("select u from AcctUser u where u.loginName=?", AcctUser.class,
				"huangjian");
		log.info(li);
		AcctUser user = new AcctUser();
		user.setEmail("hj2@powerlong.com");
		user.setLoginName("huangjian7");
		user.setPassword("123");
		// em.persist(user);
//		accountManager.saveUser(user);
		// accountManager.delete(user);
		// User u=baseService.find(User.class, 1l);
		// User user2 = em.merge(user);
		// user.setName("黃鍵");
		AcctUser u = commonService.findByPK(AcctUser.class, 37l);
		u.setEmail("hj37@powerlong.com");
		accountManager.saveUser(u);
		log.info(u);
		u = accountManager.getUser(16l);
		log.info(u);
		// accountManager.delete(u);
		// u.setEmail("test2@hj.com");
		// accountManager.saveUser(u);
		return super.execute();
	}

	public String list() throws Exception {
		// users = accountManager.getAllUser();
		page = accountManager.getPageUser();
		return "list";
	}

}
